// Import API configuration if running in Node.js environment
// In browser, make sure config.js is included before this file
if (typeof API_CONFIG === 'undefined' && typeof require !== 'undefined') {
    const API_CONFIG = require('./config.js');
}

// User service functions
const UserService = {
    // Get user profile
    getMyProfile: async function() {
        try {
            const token = localStorage.getItem('token') || '';
            const response = await fetch(API_CONFIG.USER_API.MY_PROFILE(), {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'token': token
                }
            });
            return await response.json();
        } catch (error) {
            console.error('Error fetching user profile:', error);
            return { success: false, message: 'Failed to load profile' };
        }
    },

    // Login function
    login: async function(username, password) {
        try {
            const response = await fetch(API_CONFIG.getFullUrl(API_CONFIG.ENDPOINTS.USER + '/login'), {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            });
            return await response.json();
        } catch (error) {
            console.error('Error during login:', error);
            return { success: false, message: 'Login failed' };
        }
    },

    // Add other user-related functions as needed
};

// Export for use in other files
if (typeof module !== 'undefined' && module.exports) {
    module.exports = UserService;
} 